﻿using Microsoft.Extensions.DependencyInjection;
using Modules.Payroll;
using Modules.Repositories;
using Pipelines.Sockets.Unofficial.Arenas;
using PmSoft;
using PmSoft.Exceptions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Modules;

/// <summary>
/// 发放工资操作日志服务
/// </summary>
[UseDI(ServiceLifetime.Scoped)]
public class WagePayrollLogService
{
    private readonly WagePayrollLogRepository wagePayrollLogRepository;

    public WagePayrollLogService(WagePayrollLogRepository wagePayrollLogRepository)
    {
        this.wagePayrollLogRepository = wagePayrollLogRepository;
    }

    /// <summary>
    /// 发放工资操作日志表单详情
    /// </summary>
    /// <param name="id">主键</param>
    /// <returns></returns>
    public WagePayrollLog? Get(int id)
    {
        return wagePayrollLogRepository.Get(id);
    }

    /// <summary>
    /// 批量删除，根据id
    /// </summary>
    /// <param name="ids">id的集合</param>
    public void Deleted(IEnumerable<int> ids)
    {
        foreach (int id in ids)
        {
            Delete(id);
        }
    }

    /// <summary>
    /// 创建操作日志
    /// </summary>
    /// <param name="wagePayroll"></param>
    /// <param name="operationType"></param>
    public void Create(WagePayroll wagePayroll, PayrollConstants.代发操作类型 operationType, string tranNo)
    {
        var log = new WagePayrollLog();
        log.OperationType = operationType;
        log.TranNo = tranNo;
        log.WageId = wagePayroll.Id;
        log.ContractNo = wagePayroll.ContractNo ?? string.Empty;
        log.BankPaymentNo = wagePayroll.BankPaymentNo ?? string.Empty;
        log.BatchNo = wagePayroll.BatchNo ?? string.Empty;
        log.CreateTime = DateTime.Now; ;
        Create(log);
    }

    /// <summary>
    /// 单个删除
    /// </summary>
    /// <param name="id">主键</param>
    public void Delete(int id)
    {
        wagePayrollLogRepository.DeleteByEntityId(id);
    }

    /// <summary>
    /// 创建发放工资操作日志
    /// </summary>
    /// <param name="entity">实体对象</param>
    public void Create(WagePayrollLog entity)
    {
        wagePayrollLogRepository.Insert(entity);
    }

    /// <summary>
    /// 编辑发放工资操作日志
    /// </summary>
    /// <param name="entity">实体对象</param>
    public void Update(WagePayrollLog entity)
    {
        wagePayrollLogRepository.Update(entity);
    }
}